package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author WeitenHong
 * @date 2020/10/16 10:38
 */

@RestController
@Anoymous
public class ShoppingController {

    @Reference(check = false)
    IHomeService iHomeService;

    @Reference(check = false)
    IContentService iContentService;

    @Reference(check = false)
    IProductCateService iProductCateService;

    @Reference(check = false)
    IProductService iProductService;

    @GetMapping("/shopping/homepage")
    public ResponseData homePage() {
        HomePageResponse homepage = iHomeService.homepage();
        List<PanelDto> panelContentItemDtos = homepage.getPanelContentItemDtos();
        ResponseData responseData = new ResponseUtil().setData(panelContentItemDtos);
        return responseData;
    }

    @GetMapping("/shopping/navigation")
    public ResponseData navigation() {
        List<PanelContentDto> result = iHomeService.getNavigation();
        ResponseData responseData = new ResponseUtil().setData(result);
        return responseData;
    }

    @GetMapping("/shopping/categories")
    public ResponseData categories(Map<String, String> map) {
        AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
        String sort = map.get("sort");
        allProductCateRequest.setSort(sort);
//        AllProductCateResponse result = iProductCateService.getAllProductCate(allProductCateRequest);
//        return new ResponseUtil<>().setData(result);

        //修改
        AllProductCateResponse result = iProductCateService.getAllProductCate(allProductCateRequest);
        List<ProductCateDto> productCateDtoList = result.getProductCateDtoList();
        return new ResponseUtil<>().setData(productCateDtoList);
    }

    //查看商品详情
    @GetMapping("/shopping/product/{id}")
    public ResponseData product(@PathVariable long id) {
        ProductDTORequest productDTORequest = new ProductDTORequest();
        productDTORequest.setId(id);
        ProductDetailDto result = iProductService.getProductDetailDTO(productDTORequest);
        return new ResponseUtil().setData(result);
    }

    //分页查询商品列表
    @GetMapping("/shopping/goods")
    public ResponseData goods(Integer page,Integer size,String sort,Long cid,Integer priceGt,Integer priceLte) {
        AllProductResponse allProductResponse=iProductService.getGoods(page,size,sort,cid,priceGt,priceLte);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",allProductResponse.getProductDtoList());
        map.put("total",allProductResponse.getTotal());
        return new ResponseUtil().setData(map);
    }
}
